package com.atom.common.remote.palace;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.activiti.engine.impl.util.json.JSONException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atom.common.CommonConstant;
import com.atom.common.exception.AvensRemoteDatabaseDaoException;
import com.atom.common.util.BeanToMapUtil;
import com.atom.common.util.CharSetUtil;
import com.atom.common.util.HttpUtil;
import com.atom.common.util.JsonResultParser;
import com.atom.common.util.JsonUtil;
import com.atom.common.util.GlobalConfigReader;
import com.atom.common.util.TicketUtil;

@Service
public class RemoteRunningTaskDao {
  
  private static Logger logger = Logger.getLogger(RemoteRunningTaskDao.class);

  @Autowired(required = false) HttpSession session;

  private static final String HOST = GlobalConfigReader.getStringValue(CommonConstant.PALACE_SERVERNAME);
  
  public void save(RemoteRunningTaskDO instance) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "todo.addTodoTask.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      Map<String, String> remoteRunningTaskToMap = BeanToMapUtil.toMap(instance);
      params.putAll(remoteRunningTaskToMap);
      String result = CharSetUtil.decodeUnicode(HttpUtil.post(finalUrl, params));
      JsonResultParser.parse(result);
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("Save RemoteRunningTaskDO Failed");
    }
  }

  public void saveAll(List<RemoteRunningTaskDO> instances) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "todo.addTodoTasksByBatch.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);  
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      // TODO params add
      String result = CharSetUtil.decodeUnicode(HttpUtil.post(finalUrl, params));
      JsonResultParser.parse(result);
    } catch (JSONException e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("SaveAll RemoteRunningTaskDO Failed");
    }
  }

  public void delete(String ActivityEntityID, String userId, String SysModId) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "todo.deleteTodoTask.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      params.put("ActivityEntityID", ActivityEntityID);
      params.put("Ownerid", userId);
      params.put("SysModId", SysModId);
      String result = CharSetUtil.decodeUnicode(HttpUtil.get(finalUrl, params));
      JsonResultParser.parse(result);
    } catch (JSONException e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("Delete RemoteRunningTaskDO Failed");
    }
  }
  
  public void updateTaskName(String sysModID, String activityEntityID, String taskName) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "todo.updateTodoTasks.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      Map<String, String> entity = new HashMap<String, String>();
      List<Map<String, String>> data = new ArrayList<Map<String,String>>();
      Map<String, List<Map<String, String>>> list = new HashMap<String, List<Map<String, String>>>();
      entity.put("sysModID", sysModID);
      entity.put("activityEntityID", activityEntityID);
      entity.put("taskName", taskName);      
      data.add(entity);      
      list.put("list", data);
      params.put("data", JsonUtil.ObjectToJsonQuietly(list));
      String result = CharSetUtil.decodeUnicode(HttpUtil.post(finalUrl, params));
      JsonResultParser.parse(result);
    } catch (JSONException e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("updateTodoTasks Failed");
    }
  }
  
  public void updateTodoTasksForDelegate(List<Map<String, String>> data, String delegateAction) {
    if (data == null || data.isEmpty()) return;
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "todo.updateTodoTasksForDelegate.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      Map<String, Object> list = new HashMap<String, Object>();
      list.put("list", data);
      list.put("delegateAction", delegateAction);
      params.put("data", JsonUtil.ObjectToJsonQuietly(list));
      String result = CharSetUtil.decodeUnicode(HttpUtil.post(finalUrl, params));
      JsonResultParser.parse(result);
    } catch (JSONException e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("updateTodoTasksForDelegate Failed");
    }
  }

}
